package com.qk.management.controller;

import com.qk.management.service.impl.FileCleanStatusService;
import com.qk.management.service.impl.OssFileCleanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class FileCleanScheduler {

    @Autowired
    private OssFileCleanService ossFileCleanService;

    @Autowired
    private FileCleanStatusService fileCleanStatusService;

    // 每天30s执行一次
    @Scheduled(cron = "*/30 * * * * ?")
    public void dailyFileClean() {
        try {
            // 获取上次清理时间
            LocalDateTime lastCleanTime = fileCleanStatusService.getLastCleanTime();

            // 执行清理
            ossFileCleanService.cleanFiles(lastCleanTime);

            // 更新清理时间
            fileCleanStatusService.updateLastCleanTime(LocalDateTime.now());
        } catch (Exception e) {
            System.err.println("定时清理任务执行失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
}